package org.jeecg.modules.online.cgform.converter;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ConvertUtil {

    /**text转value*/
    public static final int in = 2;
    /**value转text*/
    public static final int out = 1;

    /**
     * 转换
     * @param flag 1value转text 2text转value
     * @param dataList
     * @param fieldList
     */
    public static void doConvertDataList(int flag,List<Map<String,Object>> dataList,List<OnlCgformField> fieldList){
        Map<String,FieldCommentConverter> converterMap = ConvertFactory.getAllConverterByFieldConfig(fieldList);
        for(Map<String,Object> map:dataList){
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            //map循环中无法新增元素 特加此otherValue作为新增元素存储的容器
            Map<String,Object> otherValue = new HashMap<String,Object>();
            while(it.hasNext()){
                Map.Entry<String, Object> entry=it.next();
                Object temp =entry.getValue();
                if(temp==null){
                    continue;
                }
                String key = entry.getKey();
                FieldCommentConverter converter = converterMap.get(key);
                if(converter!=null){
                    String tempString = temp.toString();
                    String real = flag==out?converter.converterToTxt(tempString):converter.converterToVal(tempString);
                    //System.out.println("原来的值="+tempString);
                    //System.out.println("替换的值="+real);
                    linkDownConfigHandler(converter,map,flag);
                    treeCategoryConfigHandler(converter,otherValue,tempString);
                    map.put(key,real);
                }
            }
            for(String key:otherValue.keySet()){
                map.put(key,otherValue.get(key));
            }
        }
    }

    /**
     * 级联下拉额外字段处理逻辑
     * @param converter
     * @param data
     * @param flag
     */
    private static void linkDownConfigHandler(FieldCommentConverter converter,Map<String,Object> data,int flag){
        Map<String,String> special = converter.getConfig();
        if(special!=null){
            String linkField = special.get("linkField");
            if(oConvertUtils.isNotEmpty(linkField)){
                for(String lf: linkField.split(",")){
                    Object temp = data.get(lf);
                    if(temp==null){
                        continue;
                    }
                    String tempString = temp.toString();
                    String real = flag==1?converter.converterToTxt(tempString):converter.converterToVal(tempString);
                    data.put(lf,real);
                }
            }
        }
    }

    /**
     * 分类字典额外字段处理逻辑
     * @param converter
     * @param otherValue
     * @param currValue
     */
    private static void treeCategoryConfigHandler(FieldCommentConverter converter,Map<String,Object> otherValue,String currValue){
        Map<String,String> special = converter.getConfig();
        if(special!=null){
            //分类字典树额外字段处理逻辑
            String treeText = special.get("treeText");
            if(oConvertUtils.isNotEmpty(treeText)){
                otherValue.put(treeText,currValue);
            }
        }
    }

}
